package com.sample;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * This is a sample class to launch a rule.
 */
public class DroolsTest {
	static Plantacao plantacao;
	static TelaInicial tInicial;
	static StatefulKnowledgeSession ksession;
	static KnowledgeRuntimeLogger logger;

	public static void main(String[] args) throws Exception {
		
		try {
			// load up the knowledge base
			KnowledgeBase kbaseBase = readKnowledgeBase();
			ksession = kbaseBase
					.newStatefulKnowledgeSession();
			logger = KnowledgeRuntimeLoggerFactory
					.newFileLogger(ksession, "test");
			plantacao = new Plantacao();
			
			tInicial = new TelaInicial("Admin");
			tInicial.btSimular.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					plantacao.setHectare(Double.parseDouble(tInicial.fieldTerreno.getText()));
					plantacao.setIrrigacao(tInicial.checkIrrigacao.isSelected());
					plantacao.setMaquinario(tInicial.checkMaquinario.isSelected());
					plantacao.setInseticida(new Inseticida(Double
							.parseDouble(tInicial.fInseticida.getText())));
					plantacao.setHerbicida(new Herbicida(Double.parseDouble(tInicial.fHerbicida
							.getText())));
					plantacao.setSemente(new SementeFeijao(Double
							.parseDouble(tInicial.fSemente.getText())));
					plantacao.setEmbalagem(new Embalagem(Double.parseDouble(tInicial.fEmbalagem
							.getText())));
					plantacao.setP2o5(new P2O5(Double.parseDouble(tInicial.fP2o5.getText())));
					plantacao.setK2o(new K2O(Double.parseDouble(tInicial.fK2o.getText())));
					plantacao.setNitro(new Nitrogenio(Double.parseDouble(tInicial.fNitorgenio
							.getText())));
					plantacao.setEnergia(new Energia(Double.parseDouble(tInicial.fEnergia
							.getText())));
					plantacao.setHm(new HoraMaquina(Double.parseDouble(tInicial.fHm.getText())));
					plantacao.setDm(new DiaHomem(Double.parseDouble(tInicial.fDh.getText())));
					plantacao.setAtivar(true);
					
					new Resposta(plantacao);
					ksession.insert(plantacao);
					ksession.fireAllRules();
					logger.close();
				}
			});
			
			
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("baseBeans.drl"),
				ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error : errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}

}
